//
//  ICMusicPlayNarrowView.m
//  DWTeacher
//
//  Created by icochu on 2022/02/22.
//  Copyright © 2022年 wlf. All rights reserved.
//

#import "ICMusicPlayNarrowView.h"
#import "AutoScrollLabel.h"
#import "UIView+XHAdd.h"
#import "XHComMacro.h"
#import "Common.h"
#import "PlayListView.h"
#import "MusicAlbumView.h"
@interface ICMusicPlayNarrowView()

@property (nonatomic,strong)CABasicAnimation *basicAnimation;

@property(nonatomic, strong) AutoScrollLabel *titleLable;

@property(nonatomic,strong)UIButton *imageView;

@property(nonatomic, strong) UILabel *timeLable;

@property(nonatomic, strong) UIButton *closeBtn;

@property(nonatomic, strong) UIButton *nextBtn;

@property(nonatomic, strong) UIView *backView;
@property (nonatomic, strong) MusicAlbumView *musicAlbum;

@end

@implementation ICMusicPlayNarrowView

- (instancetype)initWithFrame:(CGRect)frame MusicModel:(ICMusicPlayModel *)model{
    if (self = [super initWithFrame:frame]) {
        _currentPlayModel = model;
        [self initView];
        self.backgroundColor = [UIColor clearColor];
    
    }
    return self;
}



- (void)initView {
    
    _bgImageView = [[UIImageView alloc] init];
    _bgImageView.userInteractionEnabled = YES;
    _bgImageView.size = CGSizeMake(SCREEN_WIDTH - (13 + 50 / 2), 50);
    _bgImageView.top = 0;
    _bgImageView.left = 13 + 50 / 2;
//    _bgImageView.image = [UIImage imageNamed:@"narrow_bgView"];
    [self addSubview:_bgImageView];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    backView.backgroundColor = [UIColor colorWithHex:@"#F4F4F4"];
    [self addSubview:backView];
    self.backView = backView;
    
    _leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 37, 13, 20, 24)];
//    _leftBtn.layer.cornerRadius = 30;
//    _leftBtn.layer.masksToBounds = YES;
    [_leftBtn setImage:IMAGE(@"narrow_left_btn") forState:UIControlStateNormal];
    [_leftBtn addTarget:self action:@selector(listBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:_leftBtn];
    
    _imageView = [UIButton new];
    _imageView.layer.cornerRadius = 50 / 2;
    _imageView.layer.masksToBounds = YES;
    _imageView.size = CGSizeMake(50, 50);
    _imageView.top = 0;
    _imageView.left = 13;
    [_imageView addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [_imageView sd_setImageWithURL:[NSURL URLWithString:_currentPlayModel.image] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"default"]];
//    [_imageView sd_setImageWithURL:[NSURL URLWithString:_currentPlayModel.image] placeholderImage:[UIImage imageNamed:@"default"]];
    [self addSubview:_imageView];
    
    _titleLable = [[AutoScrollLabel alloc]initWithFrame:CGRectMake(69, 5, 120, 20)];
    
    _titleLable.font = [UIFont systemFontOfSize:14];
    _titleLable.textColor = [UIColor blackColor];
    _titleLable.scrollSpeed = 30;
    _titleLable.textAlignment = NSTextAlignmentLeft;
    _titleLable.fadeLength = 12.f;
    _titleLable.scrollDirection = CBAutoScrollDirectionLeft;
    [_titleLable observeApplicationNotifications];
    _titleLable.text = _currentPlayModel.name;
    [backView addSubview:_titleLable];
    
    _timeLable = [[UILabel alloc] initWithFrame:CGRectMake(69, 28, 120, 22)];
    _timeLable.font = FONT(9);
    _timeLable.textColor = [UIColor colorWithHex:@"#666666"];
    _timeLable.userInteractionEnabled = YES;
    _timeLable.text = self.currentPlayModel.media_class_name;
//    [Common updataTimerLableWithLable:_timeLable Second:_currentPlayModel.audioLength];
    [backView addSubview:_timeLable];
    
    UIView *prisentView = [[UIView alloc] initWithFrame:CGRectMake(56, 0, SCREEN_WIDTH - 56 - 92, 50)];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(_bgImageViewTapAction)];
    [prisentView addGestureRecognizer:tap];
    [backView addSubview:prisentView];
    
    HWCircleView *circleView = [[HWCircleView alloc] init];
    circleView.progerWidth = 3;
    circleView.progerssBackgroundColor = [UIColor colorWithHex:@"#F4F4F4"];
    circleView.size =  CGSizeMake(28, 28);
    circleView.top = 11;
    circleView.right = _bgImageView.right - 62;
    self.circleView = circleView;
    self.circleView.progerssColor = [UIColor colorWithHex:@"#0091FF"];
    [backView addSubview:circleView];
    
    self.loadingView = [[PlayLoadingView  alloc] init];
    self.loadingView.size =  CGSizeMake(28, 28);
    self.loadingView.top = 11;
    self.loadingView.right = _bgImageView.right - 62;
    self.loadingView.hidden = YES;
    [backView addSubview:self.loadingView];
    
    
    _playBtn = [UIButton new];
    _playBtn.size = CGSizeMake(25, 25);
    _playBtn.top = 25.0 / 2.0;
    _playBtn.right = _bgImageView.right - 62;
    [_playBtn setImage:IMAGE(@"narrow_playBtn_play") forState:UIControlStateNormal];
    [_playBtn setImage:IMAGE(@"narrow_playBtn_puse") forState:UIControlStateSelected];
    [_playBtn addTarget:self action:@selector(playBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:_playBtn];
    
}
- (void)listBtnAction:(UIButton *)button {
    if (self.listBtnAction){
        self.listBtnAction();
    }
}
- (void)leftBtnAction:(UIButton *)button {
    if (self.showState == ViewShow || self.showState == ViewBegin) {
        if(self.leftBtnAction) self.leftBtnAction(YES);
        self.backView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
        _bgImageView.backgroundColor = [UIColor colorWithHex:@"#F4F4F4"];
    }else{
        if(self.leftBtnAction) self.leftBtnAction(NO);
        self.backView.backgroundColor = [UIColor colorWithHex:@"#F4F4F4"];
        _bgImageView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
    }
   
}

- (void)_bgImageViewTapAction {
    if(self.bgImageViewTapAction) self.bgImageViewTapAction();
}

- (void)playBtnAction:(UIButton *)button{
    button.selected = !button.selected;
    if(self.playBtnAction) self.playBtnAction(button.selected);
}

//- (void)closeButtonAction {
//    if(self.closeBtnAction) self.closeBtnAction();
//}

//- (void)nextButtonAction{
//    if (self.nextBtnAction) {
//        self.nextBtnAction();
//    }
//}

- (void)beginImageViewAnimate {
    [self.imageView.layer removeAllAnimations];//移除动画
    self.basicAnimation = nil;
    [self basicAnimation];//开始动画
    self.isRunningAnimate = YES;
    self.closeBtn.hidden = YES;
}

- (void)continueImageViewAnimate {
    _playBtn.selected = NO;
    //得到view当前动画时间偏移量
    CFTimeInterval stopTime = [self.imageView.layer timeOffset];
    //初始化开始时间
    self.imageView.layer.beginTime = 0;
    //初始化时间偏移量
    self.imageView.layer.timeOffset = 0;
    //设置动画速度
    self.imageView.layer.speed = 1;
    //计算时间差
    CFTimeInterval tempTime = [self.imageView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - stopTime;
    //重新设置动画开始时间
    self.imageView.layer.beginTime = tempTime;
    self.closeBtn.hidden = YES;
}

- (void)stopImageViewAnimate {
    
    self.closeBtn.hidden = NO;
    
    self.isRunningAnimate = NO;
    _playBtn.selected = YES;
    CFTimeInterval stopTime = [self.imageView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    //停止动画，速度设置为0
    self.imageView.layer.speed = 0;
    //设置时间偏移量
    self.imageView.layer.timeOffset = stopTime;
}

- (void)updateNewMusicWithModel:(ICMusicPlayModel *)model IsPlaying:(BOOL)isPlay{
    if (model != nil) {
        _currentPlayModel = model;
        _titleLable.text = model.name;
//        [_imageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"default"]];
        [_imageView sd_setImageWithURL:[NSURL URLWithString:_currentPlayModel.image] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"default"]];
        _timeLable.text = model.media_class_name;
        _playBtn.selected = !isPlay;
    }
}

- (CABasicAnimation *)basicAnimation {
    if (_basicAnimation == nil) {
        self.basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        //旋转一圈时长
        self.basicAnimation.duration = 30;
        //开始动画的起始位置
        self.basicAnimation.fromValue = [NSNumber numberWithInt:0];
        //M_PI是180度
        self.basicAnimation.toValue = [NSNumber numberWithInt:M_PI*2];
        //动画重复次数
        [self.basicAnimation setRepeatCount:NSIntegerMax];
        //播放完毕之后是否逆向回到原来位置
        [self.basicAnimation setAutoreverses:NO];
        //是否叠加（追加）动画效果
        [self.basicAnimation setCumulative:YES];
        //停止动画，速度设置为0
        self.imageView.layer.speed = 1;
        //    self.ImageView.layer.speed = 0;
        [self.imageView.layer addAnimation:self.basicAnimation forKey:@"basicAnimation"];
        
    }
    return _basicAnimation;
}

@end
